<?php

class Model_CustomerConfig extends Model_Table {
	
	protected $_name = 'customer_config';
	
    public function getToken($customer_id){
        $row=$this->fetchRow("select * from ".$this->_name." where customer_id='{$customer_id}'");
        if($row['appid'] && $row['appsecret']){
            if($row['valid_time'] > time() && $row['token']){//未过期
                return $row['token'];
            }else{
                $token=$this->getAccessToken($row['appid'],$row['appsecret']);
                if($token){
                    $data=array();
                    $data['token']=$token;
                    $data['valid_time']=time()+1800;	
                    $this->updateById($row['id'],$data);
                }
                return $token;
            }
        }else{//没有配置appid和appsecret
            return false;
        }
	}
	
    //获取用户分组
    public function getGroups($customer_id){
        $token=$this->getToken($customer_id);
        return $this->getUserGroups($token);
	}
    
    /**
     * 以下方法全部是微信接口
     */
    
    //微信接口获取access_token
    public function getAccessToken($appid,$appsecret){
        if($appid && $appsecret){
            $jsonInfo=file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret);
            $objInfo=json_decode($jsonInfo);
            return $objInfo->access_token;
        }else{
            return false;
        }
	}
    
    //微信接口获取用户基本信息
    public function getUserInfo($token,$openid){
        if($token && $openid){
            $jsonInfo=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$openid."&lang=zh_CN");
            $info=json_decode($jsonInfo,true);
            return $info;
        }else{
            return false;
        }
	}
    
    //微信接口创建用户分组
    public function createUserGroups($token,$name){
        if($token && $name){
            $url=("https://api.weixin.qq.com/cgi-bin/groups/create?access_token=".$token);
            $data='{"group":{"name":"'.$name.'"}}';
            $jsonInfo=curl_request($url,$data);
            $info=json_decode($jsonInfo,true);
            return $info['group'];
        }else{
            return false;
        }
	}
    
    //微信接口修改分组名
    public function editUserGroups($token,$id,$name){
        if($token && $name){
            $url=("https://api.weixin.qq.com/cgi-bin/groups/update?access_token=".$token);
            $data='{"group":{"id":'.$id.',"name":"'.$name.'"}}';
            $jsonInfo=curl_request($url,$data);
            $info=json_decode($jsonInfo,true);
            $res=$info['errmsg']=='ok' ? true : false;
            return $res;
        }else{
            return false;
        }
	}
    
    //微信接口删除用户分组
    public function delUserGroups($token,$id){
        if($token){
            $url=("https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=".$token);
            $data='{"group":{"id":'.$id.'}}';
            $jsonInfo=curl_request($url,$data);
            $info=json_decode($jsonInfo,true);
            $res=$info['errmsg']=='ok' ? true : false;
            //return $res;
            //因为微信接口bug，直接返回true
            return true;
        }else{
            return false;
        }
	}
    
    //微信接口获取用户分组
    public function getUserGroups($token){
        if($token){
            $jsonInfo=file_get_contents("https://api.weixin.qq.com/cgi-bin/groups/get?access_token=".$token);
            $info=json_decode($jsonInfo,true);
            return $info['groups'];
        }else{
            return false;
        }
	}
    
    //微信接口批量移动用户分组
    public function changeUserGroups($token,$openid=array(),$groupid=0){
        if($token && $openid){
            $url=("https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=".$token);
            $arr=array();
            $arr['openid_list']=$openid;
            $arr['to_groupid']=(int)$groupid;
            $data=json_encode($arr);
            
            $jsonInfo=curl_request($url,$data);
            $info=json_decode($jsonInfo,true);
            $res=$info['errmsg']=='ok' ? true : false;
            return $res;
        }else{
            return false;
        }
	}
    
}